package com.shepin.shechipin;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.shepin.R;

/*
 奢侈品分类
 */
public class SheChiPinActivity extends FragmentActivity implements
        OnClickListener {
	// 获取布局文件里的控件
	private TextView ShouBiao, ZhuBao, JiaJu, Bao, MingJiu, XueJia, HuaZhuang,
			Che, ShouShi;
	// 控制碎片的控件
	private FragmentManager manager;
	private FragmentTransaction transaction;

	private int xuanZhe;// 碎片每个按钮的参数

	private ImageView SFanHui;//

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.she_chi_pin);
		// 获取FragmentManager
		manager = getSupportFragmentManager();
		//获取返回控件
		SFanHui = (ImageView) findViewById(R.id.SFanHui);
		// 获取控件
		ShouBiao = (TextView) findViewById(R.id.ShouBiao);
		ZhuBao = (TextView) findViewById(R.id.ZhuBao);
		JiaJu = (TextView) findViewById(R.id.JiaJu);
		Bao = (TextView) findViewById(R.id.Bao);
		MingJiu = (TextView) findViewById(R.id.MingJiu);
		XueJia = (TextView) findViewById(R.id.XueJia);
		HuaZhuang = (TextView) findViewById(R.id.HuaZhuang);
		Che = (TextView) findViewById(R.id.Che);
		ShouShi = (TextView) findViewById(R.id.ShouShi);
		//给空间添加事件
		ShouBiao.setOnClickListener(this);
		ZhuBao.setOnClickListener(this);
		JiaJu.setOnClickListener(this);
		Bao.setOnClickListener(this);
		MingJiu.setOnClickListener(this);
		XueJia.setOnClickListener(this);
		HuaZhuang.setOnClickListener(this);
		Che.setOnClickListener(this);
		ShouShi.setOnClickListener(this);
		//添加返回控件
		SFanHui.setOnClickListener(this);

		Shangping(1);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.SFanHui:
			//添加返回按钮的事件
			finish();
			break;
		case R.id.ShouBiao:
			// 碎片添加事件
			Shangping(1);
			break;
		case R.id.ZhuBao:
			Shangping(2);
			break;
		case R.id.JiaJu:
			Shangping(3);
			break;
		case R.id.Bao:
			Shangping(4);
			break;
		case R.id.MingJiu:
			Shangping(5);
			break;
		case R.id.XueJia:
			Shangping(6);
			break;
		case R.id.HuaZhuang:
			Shangping(7);
			break;
		case R.id.Che:
			Shangping(8);
			break;
		case R.id.ShouShi:
			Shangping(9);
			break;
		}
	}

	private void Shangping(int BaId) {
		if (BaId == xuanZhe) {
			return;
		}
		//保持原子性
		transaction = manager.beginTransaction();
		// 获取碎片
		Fragment fra = manager.findFragmentByTag("sp" + BaId);
		if (fra == null) {
			if (BaId == 1) {
				fra = new ShouBiaoActivity();
			} else if (BaId == 2) {
				fra = new ZhuBaoActivity();
			} else if (BaId == 3) {
				fra = new JiaJuActivity();
			} else if (BaId == 4) {
				fra = new BaoActivity();
			} else if (BaId == 5) {
				fra = new JiuActivity();
			} else if (BaId == 6) {
				fra = new XueJiaActivity();
			} else if (BaId == 7) {
				fra = new HuaZhuangActivity();
			} else if (BaId == 8) {
				fra = new CheActivity();
			} else if (BaId == 9) {
				fra = new ShouShiActivity();
			}
			transaction.add(R.id.xuanzhe, fra, "sp" + BaId);
		} else {
			if (fra.isDetached()) {
				transaction.attach(fra);
			}
		}
		for (int i = 1; i <= 9; i++) {
			Fragment f = manager.findFragmentByTag("sp" + i);
			if (i == BaId) {
				continue;
			} else {
				if (f != null) {
					transaction.detach(f);
				}
			}
		}
		// 提交当前碎片
		transaction.commitAllowingStateLoss();
		// 让碎片知道当前选着的id
		xuanZhe = BaId;
	}
}
